# -*- coding:utf-8 -*-


import unittest
import selenium.common
from selenium import webdriver
from WebautoTest.base.base_yaml import get_yaml
import os


def get_driver():
    """
    初始化浏览器
    """
    try:
        # 配置浏览器驱动路径
        chromedriver = "../chromedriver.exe"
        # 将浏览器驱动路径赋值给环境变量
        os.environ["webdriver.chrome.driver"] = chromedriver
        # 初始化一个浏览器
        driver = webdriver.Chrome(chromedriver)
        # 窗口最大化
        driver.maximize_window()
        # 查找url
        openurl = get_yaml("../common/config.yaml")
        # 打开url
        driver.get(openurl["url"])
        # 声明全局隐式等待时间
        driver.implicitly_wait(10)
        return driver

    except selenium.common.exceptions.SessionNotCreatedException:
        print("驱动版本呢不对去http://chromedriver.storage.googleapis.com/index.html下载")
        return False


class ParametrizedTestCase(unittest.TestCase):
    """
    参数化测试用例
    """
    def __init__(self, method_name='runTest.py_name'):
        super(ParametrizedTestCase, self).__init__(method_name)

    @classmethod
    def setUpClass(cls):
        pass
        cls.driver = get_driver()

    def setUp(self):
        pass

    @classmethod
    def tearDownClass(cls):
        cls.driver.close()
        cls.driver.quit()
        pass

    def tearDown(self):
        pass

    # 静态方法装饰器
    @staticmethod
    def parametrize(testcase_file_name):
        """
            参数化方法
        """
        # getTestCaseNames方法获取测试用例的排序序列
        test_loader = unittest.TestLoader()
        test_names = test_loader.getTestCaseNames(testcase_file_name)
        # 创建测试套件
        suite = unittest.TestSuite()
        for name in test_names:
            # suite.addTest(testcase_file_name(name, param=param))
            suite.addTest(testcase_file_name(method_name=name))
        return suite
